package com.longrise.co.ui.carfiles.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.co.R;
import com.longrise.co.api.CommonPara;
import com.longrise.co.ui.carfiles.contract.CarFilesContract;
import com.longrise.co.ui.carfiles.model.CarFilesModel;
import com.longrise.co.ui.carfiles.model.RxRepairRecordBean;
import com.longrise.co.ui.carfiles.model.UploadBean;
import com.longrise.co.ui.carfiles.presenter.CarFilesPresenter;
import com.longrise.co.ui.carfiles.widget.EvaluationSuccessDialog;
import com.longrise.co.ui.home.model.CompanyDetail;
import com.longrise.co.utils.Constants;
import com.longrise.co.utils.StringUtils;
import com.ly.common.base.BaseFragment;
import com.ly.common.dialog.DialogOnClickListener;
import com.ly.common.dialog.MDAlertDialog;
import com.ly.common.security.EnCryptDeCrypt2;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by luoyang on 2017-7-19.
 */

public class RepairEvaluationFragment extends BaseFragment<CarFilesPresenter, CarFilesModel> implements CarFilesContract.ComplaintView<CompanyDetail>,DialogOnClickListener {

    @Bind(R.id.head_title)
    TextView headTitle;
    @Bind(R.id.complaint_name)
    TextView complaintName;
    @Bind(R.id.ratingBar)
    RatingBar ratingBar;
    @Bind(R.id.location_tv)
    TextView locationTv;
    @Bind(R.id.call_tv)
    TextView callTv;
    @Bind(R.id.very_good)
    ImageView veryGood;
    @Bind(R.id.good)
    ImageView good;
    @Bind(R.id.bad)
    ImageView bad;
    @Bind(R.id.service_attitude)
    RatingBar serviceAttitude;
    @Bind(R.id.maintenance_quality)
    RatingBar maintenanceQuality;
    @Bind(R.id.maintenance_efficiency)
    RatingBar maintenanceEfficiency;
    @Bind(R.id.maintenance_price)
    RatingBar maintenancePrice;
    @Bind(R.id.storefront_environment)
    RatingBar storefrontEnvironment;
    @Bind(R.id.evaluation_content)
    EditText evaluationContent;


    private String mCompanyId;
    private String mRepairId;
    private String mYears;
    private EvaluationSuccessDialog mDialog;

    private static final int VERY_GOOD = 1;
    private static final int GOOD = 2;
    private static final int BAD = 3;

    private int mCurrentSatisfaction = -1;

    private MDAlertDialog mCallDialog;
    private CompanyDetail mData;


    @Override
    protected int getLayoutResource() {
        return R.layout.fg_repair_evaluation;
    }

    @Override
    public void initPresenter() {
        mPresenter.setVM(this, mModel);
    }

    @Override
    protected void initView() {
        setText(headTitle, "维修评价");
//        setSatisfaction(VERY_GOOD);
        mCompanyId = getArguments().getString("companyId");
        mRepairId = getArguments().getString("repairId");
        mYears = getArguments().getString("years");

       /* serviceAttitude.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
            @Override
            public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                if(fromUser) {
                    ratingBar.setRating(rating);
                }
            }
        });*/
        mPresenter.getCompanyDetail(mCompanyId);
    }

    @Override
    public void requestSuccess(CompanyDetail companyDetail) {
        if(companyDetail == null) {
            return;
        }

        mData = companyDetail;
        setText(complaintName, companyDetail.getCompanyname());
        if(companyDetail != null && !StringUtils.isEmpty(companyDetail.getStar())) {
            ratingBar.setRating(Float.parseFloat(companyDetail.getStar()));
        }

        setText(locationTv, companyDetail.getCompanyaddress());
        setText(callTv, companyDetail.getBusphone());
    }

    @Override
    public void onComplete() {

    }

    @Override
    public void submitSuccess() {
        mRxManager.post(Constants.RX_EVENT_REPAIR_RECORD, new RxRepairRecordBean("4", null));
        mRxManager.post(Constants.RX_EVENT_REFRESH_MINE, null);
        if(mDialog == null) {
            initDialog();
        }
        mDialog.show();
    }

    @Override
    public void uploadPhotoSuccess(UploadBean bean) {

    }


    /**
     * 设置满意度
     * @param type 满意度值
     */
    private void setSatisfaction(int type) {
        clearAllSatisfaction();
        switch (type) {
            case VERY_GOOD :
                mCurrentSatisfaction = 5;
                veryGood.setImageResource(R.drawable.radio_select);
                break;
            case GOOD :
                mCurrentSatisfaction = 3;
                good.setImageResource(R.drawable.radio_select);
                break;
            case BAD :
                mCurrentSatisfaction = 1;
                bad.setImageResource(R.drawable.radio_select);
                break;
        }
    }

    private void clearAllSatisfaction() {
        veryGood.setImageResource(R.drawable.radio_unselect);
        good.setImageResource(R.drawable.radio_unselect);
        bad.setImageResource(R.drawable.radio_unselect);
    }

    public static RepairEvaluationFragment newInstance(String companyId, String repairId, String years) {
        Bundle args = new Bundle();
        args.putString("companyId", companyId);
        args.putString("repairId", repairId);
        args.putString("years", years);

        RepairEvaluationFragment fragment = new RepairEvaluationFragment();
        fragment.setArguments(args);
        return fragment;
    }

    private void initCallDialog() {
        mCallDialog = new MDAlertDialog.Builder(getContext())
                .setContentText("确定拨打该企业电话？")
                .setOnclickListener(this)
                .build();
    }

    private void initDialog() {
        mDialog = new EvaluationSuccessDialog(getContext(), R.style.dialog_style);
        mDialog.setCanceledOnTouchOutside(false);
        mDialog.setOnclickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
                //返回上一层
                _mActivity.onBackPressed();
            }
        });
        mDialog.create();
    }

    private EntityBean getRequestPara() {
        EntityBean bean = new EntityBean();
        try {
            String enRecordId = EnCryptDeCrypt2.getInstance().EnCrypt(mRepairId);

            bean.set("recordid", mRepairId);
            bean.set("year", mYears);
            bean.set("userflag", CommonPara.getInstance().getUserFlag());
            bean.set("areaid", CommonPara.getInstance().getAreaId());
            //1 评价 0 追评
            bean.set("type", "1");
            bean.set("evaluatedetails", evaluationContent.getText().toString());
            bean.set("evaluatetotle", mCurrentSatisfaction);

            bean.set("evaluateservice",serviceAttitude.getRating());
            bean.set("evaluatequality",maintenanceQuality.getRating());
            bean.set("evaluateefficiency",maintenanceEfficiency.getRating());
            bean.set("evaluateprice",maintenancePrice.getRating());
            bean.set("evaluateenvironment",storefrontEnvironment.getRating());

            bean.set("showflag", "1");
            bean.set("user", CommonPara.getInstance().getUser());
            bean.set("sign", enRecordId);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return bean ;
    }


    @OnClick({R.id.head_back, R.id.very_good, R.id.good, R.id.bad, R.id.very_good_tv, R.id.good_tv, R.id.bad_tv, R.id.confirm,R.id.complaint_call_iv})
    public void onViewClicked(View view) {
        if(checkDoubleClick()) {
            switch (view.getId()) {
                case R.id.head_back:
                    _mActivity.onBackPressed();
                    break;
                case R.id.very_good:
                    setSatisfaction(VERY_GOOD);
                    break;
                case R.id.good:
                    setSatisfaction(GOOD);
                    break;
                case R.id.bad:
                    setSatisfaction(BAD);
                    break;
                case R.id.very_good_tv:
                    setSatisfaction(VERY_GOOD);
                    break;
                case R.id.good_tv:
                    setSatisfaction(GOOD);
                    break;
                case R.id.bad_tv:
                    setSatisfaction(BAD);
                    break;
                case R.id.complaint_call_iv:
                    if(mCallDialog == null) {
                        initCallDialog();
                    }
                    mCallDialog.show();
                    break;
                case R.id.confirm:
                    if(!checkEvaluation()) {
                        return;
                    }
                    EntityBean bean = getRequestPara();
                    if(bean != null) {
                        mPresenter.carEvaluation(bean);
                    }
                    break;
            }
        }

    }

    private boolean checkEvaluation() {
        if(mCurrentSatisfaction == -1) {
            showErrorTip("请选择评价满意度");
            return false;
        }
        if(StringUtils.isEmpty(evaluationContent.getText().toString())) {
            showErrorTip("请输入评价内容");
            return false;
        }
        return true;
    }

    @Override
    public void clickLeftButton(View view) {
        if(mCallDialog != null) {
            mCallDialog.dismiss();
        }
    }

    @Override
    public void clickRightButton(View view) {
        //拨打电话
        if(mData != null && !StringUtils.isEmpty(mData.getBusphone())) {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_DIAL);
            intent.setData(Uri.parse("tel:" + mData.getBusphone()));
            startActivity(intent);
        }

        if(mCallDialog != null) {
            mCallDialog.dismiss();
        }
    }
}
